<?php

define("BTNCOLOR_WHITE",	"g-button-white");
define("BTNCOLOR_BLUE",		"g-button-blue");
define("BTNCOLOR_GREEN",	"g-button-share");
define("BTNCOLOR_RED",		"g-button-red");
define("BTNCOLOR_BASE",		"");
define("BTNTARGET_BLANK",	"_blank");
define("BTNTARGET_PARENT",	"_parent");
define("BTNTARGET_NO",		"");




class SMHelper extends Helper
{
	public $admin;
	public $store;
	public $settings;
	
		
	/**
	 * Загрузка настроек магазина
	 * 
	 * @return void
	 */
	public function load_store_settings( $sid )
	{
		$array = new Ini("");
		
		$settings = ORM::for_table('settings')->where('store_id', $sid)->find_array();
		if ( $settings )
		{
			foreach( $settings as $set )
			{
				$array->set($set['group'], $set['key'], $set['value']);
			}
		}
		else
		{
			return false;
		}
		
		return $array;
	}
	
	public function setMenuActive( $name, $state = "active" )
	{
		$this->site->page->setVar( "pb_".$name, $state );
	}
	
	public function setSubmenuActive( $name, $state = "active" )
	{
		$this->site->page->setVar( "pba_".$name, $state );
	}
	
	public function addBodyTitleButton( $link, $locTitle, $color = "base", $target = false )
	{
		$cssclass = "";
		switch( $color )
		{
			case "red": $cssclass = "g-button-red"; break;
			case "blue": $cssclass = "g-button-blue"; break;
			case "green": $cssclass = "g-button-share"; break;
			case "white": $cssclass = "g-button-white"; break;
			default: break;
		}
		
		if ( $target )
		{
			$cssclass .= ' g-button-external';
		}
		
		$this->site->page->append("BodyTitleMenu", '<a href="'.$link.'" class="g-button '.$cssclass.'" target="'.$target.'">'.$this->site->locale->translate($locTitle).'</a>&nbsp;');
	}
	
	public function addBTB( $link, $title, $color = BTNCOLOR_BASE, $target = BTNTARGET_NO )
	{
		$cssclass = $color;
		if ( $target != BTNTARGET_NO )
		{
			$cssclass .= ' g-button-external';
		}
		$this->site->page->append("BodyTitleMenu", '<a href="'.$link.'" class="g-button '.$cssclass.'" target="'.$target.'">'.$this->site->locale->translate($title).'</a>&nbsp;');
	}
	
	public function defaults()
	{
		$store = ORM::for_table('store')->where('domain', getSubdomain() ?: "www")->find_one();
		$settings = $this->load_store_settings( $store['id'] );
		$admin = ORM::for_table('admins')->where('id', $_SESSION['admin_id'])->find_one();
		
		
		$this->site->page->header("control/header.tpl");
		$this->site->page->setVar( "SubDomainName", $this->site->locale->translate($store['title']) );
		$this->site->page->setVar("AdminName", $admin['name']);
		$this->site->page->load_langlist("login/langlist.tpl");
		
		if ( intval($admin['id']) == 1 )
		{
			$this->site->page->setVar("stores_access", true);
		}
		
		$adm = new AdminAccess( intval($_SESSION['admin_id']) );
		$this->page->setVar("Access", $adm);
		
		return $adm;
	}
}







